﻿using Newtonsoft.Json.Linq;

namespace PlurkApi
{
    /// <summary>
    /// the public profile of any plurk user
    /// </summary>
    public class publicProfile
    {
        #region Properties

        /// <summary>
        /// the number of user's fans
        /// </summary>
        public int fans_count { get; set; }

        /// <summary>
        /// the number of user's friends
        /// </summary>
        public int friends_count { get; set; }

        public string privacy { get; set; }

        public bool has_read_permission { get; set; }

        /// <summary>
        /// null if not logged in
        /// </summary>
        public bool? is_fan { get; set; }        

        /// <summary>
        /// null if not logged in
        /// </summary>
        public bool? are_friends { get; set; }

        /// <summary>
        /// null if not logged in
        /// </summary>
        public bool? is_following { get; set; }

        public user_info user_info { get; set; }
        public plurks plurks { get; set; }

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="publicProfile"/> class.
        /// </summary>
        /// <param name="jsonString">The json string.</param>
        public publicProfile(string jsonString)
        {
            var obj = JObject.Parse(jsonString);
            if (obj.Count <= 0){ return; }

            fans_count = (obj["fans_count"] == null) ? 0 : (int) obj["fans_count"];
            friends_count = (obj["friends_count"] == null) ? 0 : (int) obj["friends_count"];

            privacy = (obj["privacy"] == null) ? string.Empty : (string) obj["privacy"];

            has_read_permission = (obj["has_read_permission"] == null || obj["has_read_permission"].ToString() == "null") ? false : (bool)obj["has_read_permission"];

            is_fan = (obj["is_fan"] == null || obj["is_fan"].ToString() == "null") ? null : (bool?)obj["is_fan"];            
            are_friends = (obj["are_friends"] == null || obj["is_following"].ToString() == "null") ? null : (bool?)obj["are_friends"];
            is_following = (obj["is_following"] == null || obj["is_following"].ToString() == "null") ? null : (bool?)obj["is_following"];

            user_info = (obj["user_info"] == null) ? null : new user_info(obj["user_info"].ToString());
        }
    }
}